﻿using System;
using System.Collections.Generic;
using System.Text;
using MyIni;

namespace XhPaper.功能实现
{
    public class DataHx : MyAutoCadObjectBase
    {
         /// <summary>
        /// 智能传感器列间的距离
        /// </summary>
        int cgqHangjianJu;
        /// <summary>
        /// 智能传感器接口板组和智能传感器接口板之间的距离
        /// </summary>
        int zJIanju;
        IniFile m_myini;// = new IniFile(Environment.CurrentDirectory+"\\datahx.ini");
        /// <summary>
        /// 通信机接口数量
        /// </summary>
        int m_txjport_count;
        /// <summary>
        /// 通信机接口列表
        /// </summary>
        List<MyTableTxj> portList = new List<MyTableTxj>();
        /// <summary>
        /// 每个电源模块包含的智能传感器
        /// </summary>
        Dictionary<int, List<MyTableJdqMini>> cgqDic = new Dictionary<int, List<MyTableJdqMini>>();
        /// <summary>
        /// 不同设备间的连线集合
        /// </summary>
        List<MyLine> m_lianXian = new List<MyLine>();
        List<string> m_cgqNames = new List<string>();
        MyTuZhi tuzhi ;
        public DataHx(string _inifile="")
        {
            if (_inifile == "")
            {
                m_myini = new IniFile(Environment.CurrentDirectory + "\\datahx.ini");
            }
            else
            {
                m_myini = new IniFile(_inifile);
            }
            tuzhi = new MyTuZhi(0, 0, null);
            SetCfg();
            SetLianXian();
        }
        public DataHx(double left, double top,string _inifile)
        {
            if (_inifile == "")
            {
                m_myini = new IniFile(Environment.CurrentDirectory + "\\datahx.ini");
            }
            else
            {
                m_myini = new IniFile(_inifile);
            }
            tuzhi = new MyTuZhi(left, top, null);
            SetCfg();
            SetLianXian();
        }
        /// <summary>
        /// 设置各种电源连线
        /// </summary>
        private void  SetLianXian()
        {
            MyPoint p1,p2;
           
            for (int i = 0; i < m_txjport_count; i++)
            {
                #region 绘制传感器485环线
                int n = cgqDic[i].Count-1;
                if (n < 0) continue;
                p1 = portList[i].Dz1[0].P1; 
                p2 = cgqDic[i][n].Dz1.P1;
                m_lianXian.Add(new MyLine(p1, p2, this));
                p1 = portList[i].Dz1[1].P1;
                p2 = cgqDic[i][n].Dz2.P1;
                m_lianXian.Add(new MyLine(p1, p2, this));
                #endregion
               
            }
        }
        /// <summary>
        /// 得到智能传感器的编号
        /// </summary>
        /// <param name="strSec"></param>
        ///
        private List<string> GetCgqNos(string strSec)
        {
            List<string> list=new List<string>();
            string str = m_myini.GetString(strSec, "智能传感器编号", "");
            string[] strs1 = str.Split(new char[]{';'},StringSplitOptions.RemoveEmptyEntries);
            foreach (string str2 in strs1)
            {
                string[] strs2 = str2.Split(new char[] {',' }, StringSplitOptions.RemoveEmptyEntries);
                int n, n0;
                int.TryParse(strs2[0].Substring(strs2[0].IndexOf('-')+1), out n0);
                string str4=strs2[0].Substring(0,strs2[0].IndexOf('-'));
                if (strs2[1].Contains("-"))
                {
                    int n5 = strs2[1].IndexOf("-");
                    string str5 = strs2[1].Substring(n5 + 1, strs2[1].Length - n5 - 1);
                    int.TryParse(str5, out n5);
                    n = n5 - n0 + 1;
                }
                else
                {
                    int.TryParse(strs2[1], out n);
                }
                for(int i=0;i<n;i++)
                {
                    string str3=str4+"-"+n0++;
                    list.Add(str3);
                }
            }
            return list;
        }

        private Dictionary<string, string> GetCgqNos_Names(string strSec)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            List<string> list = new List<string>();
            string str = m_myini.GetString(strSec, "智能传感器编号", "");
            string[] strs1 = str.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str2 in strs1)
            {
                string[] strs2 = str2.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                int n, n0;
                int.TryParse(strs2[0].Substring(strs2[0].IndexOf('-') + 1), out n0);
                string str4 = strs2[0].Substring(0, strs2[0].IndexOf('-'));
                if (strs2[1].Contains("-"))
                {
                    int n5 = strs2[1].IndexOf("-");
                    string str5 = strs2[1].Substring(n5 + 1, strs2[1].Length - n5 - 1);
                    int.TryParse(str5, out n5);
                    n = n5 - n0 + 1;
                }
                else
                {
                    int.TryParse(strs2[1], out n);
                }
                for (int i = 0; i < n; i++)
                {
                    string str3 = str4 + "-" + n0++;
                    string str5;
                    if (strs2.Length > 2)
                    {
                        str5 = strs2[2];
                    }
                    else
                    {
                        str5 = "智能传感器";
                    }
                    dic.Add(str3, str5);
                }
            }
            return dic;
        }

        /// <summary>
        /// 读取ini文件，对485环线进行配置
        /// </summary>
        private void SetCfg()
        {
            cgqHangjianJu = m_myini.GetInt16("配置", "智能传感器列间距", 0);
            zJIanju = m_myini.GetInt16("配置", "组于组行间距", 0);
             int n1,n2;
             m_txjport_count = m_myini.GetInt16("通信机接口", "sum", 0);
            for (int i = 0; i < m_txjport_count; i++)
            {
                n1 = m_myini.GetInt16("接口" + (i + 1), "智能传感器", 0);
                n2 = m_myini.GetInt16("接口" + (i + 1), "接口板", 0);
                List<MyTableJdqMini> jdqlist = new List<MyTableJdqMini>();
                List<MyTableJkbMini> jkblist = new List<MyTableJkbMini>();
                MyTableTxj txj = new MyTableTxj(50, tuzhi.Top-i * zJIanju, tuzhi);
                if (txj.m_TableNameDic.ContainsKey("name"))
                {
                    txj.m_TableNameDic["name"].Text = "接口" + (i + 1);
                }
                portList.Add(txj);
               // List<string> strlist = GetCgqNos("接口" + (i + 1));
                Dictionary<string, string> dic = GetCgqNos_Names("接口" + (i + 1));
               // for (int j = 0; j < strlist.Count; j++)
                int j = 0;
                foreach(KeyValuePair<string,string> pair in dic)
                {
                    MyTableJdqMini jd=new MyTableJdqMini(j * cgqHangjianJu + 100, tuzhi.Top - i * zJIanju, tuzhi,false);
                    jd.m_TableNameDic["no"].Text = "板号：" + pair.Key;// strlist[j];
                    jd.m_TableNameDic["name"].Text = pair.Value;
                    jdqlist.Add(jd);
                    j++;
                }
                j = 0;
                cgqDic.Add(i, jdqlist);
            }
        }
        public override void MyDraw(MyCadDll.MyCad mycad)
        {
            base.MyDraw(mycad);
            foreach (MyTableTxj txj in portList)
            {
                txj.MyDraw(mycad);
            }
            for (int i = 0; i < m_txjport_count; i++)
            {
                foreach (MyTableJdqMini jdq in cgqDic[i])
                {
                    jdq.MyDraw(mycad);
                }
            }
            foreach (MyLine line in m_lianXian)
            {
                line.MyDraw(mycad);
            }
            
        }
    }
}
